<!doctype html>
<html>
<head>
	<meta charset="utf-8"><meta name="format-detection" content="telephone=no">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
	<title>我的</title>
	<link rel="stylesheet" type="text/css" href="../../lib/css/skin.css"/>
</head>
<body>
<div id="root">
	<div class="app_warp">
		<div class="app_head">
			<span class="left back"></span>
			<span class="name">订单详情</span>
		</div>
		<div class="app_main">
			<div class="space"></div>
			<div id="noready" class="nocontent animated fadeInUp">
				<img src="../../lib/img/loading.gif" class="minimize" alt="">
				<div class="text minimize">
					准备订单数据
				</div>
			</div>
			<div id="nocontent" class="nocontent animated fadeInUp hide">
				<img src="../../lib/img/nocomment.png" alt="">
				<div class="text">
					未能查询到订单数据
				</div>
			</div>
			<div id="user_order_view" class="user_order hide"></div>
		</div>
	</div>
</div>
<script id="user_order_view_template" type="text/html">
	<ul class="order_view">
		<li>
			<span class="key">
				订单编号
			</span>
			<span class="val">
				<%:=ordersn%>
			</span>
		</li>
		<li>
			<span class="key">
				订单状态
			</span>
			<span class="val err">
				<%:=orderStatus%>
			</span>
		</li>

		<%if(orderStatus==='待付款'){%>

		<li class="order_btns hide animated fadeIn">
			<a href="javascriptL:;" class="order_close" tapmode onclick="clearOrder()">
				取消订单
			</a>
			<a href="javascriptL:;" class="order_pay" tapmode onclick="payOrder()">
				立即支付
			</a>
		</li>

		<li class="order_btns space"></li>

		<%}else{%>
			<li class="space"></li>
		<%}%>

		<li>
			<span class="name">
				约课详情
			</span>
		</li>
		<li>
			<span class="key">
				课程名称
			</span>
			<span class="val">
				<%:=lname%>
			</span>
		</li>
		<li>
			<span class="key">
				课程费用
			</span>
			<span class="val">
				<%:=price%>元
			</span>
		</li>

		<li class="space"></li>

		<li>
			<span class="name">
				机构
			</span>
		</li>
		<li class="col">
			<img src="<%:=mavatar%>" alt="" class="order_cover imagecache">
			<span class="tit">
				<%:=mname%>
			</span>
			<span class="intro">
				<%:=area%> / <% for(var i in direction){ %><%:=direction[i]%> <%}%>
			</span>
		</li>

		<li class="space"></li>

		<li>
			<span class="name">
				学生
			</span>
		</li>

		<li class="col">
			<img src="<%:=studentAvatar%>" alt="" class="order_cover imagecache">
			<span class="tit tit_c">
				<%:=studentName%>
			</span>
		</li>

		<li class="space"></li>

		<li>
			<span class="key">
				咨询客服
			</span>
			<span class="val">
				<a class="tel" href="tel:<%:=tel%>"><%:=tel%></a>
			</span>
		</li>
	</ul>
</script>
<script>
	var 		//下单提交动作
		payIng = false,
		//跳转订单列表的动作
		getOverPage = function () {
			var winName = api.winName;

			_$.sleep(function () {
				api.execScript({
					name: 'index',
					frameName: 'user',
					script: 'getPage("dingdan")'
				})
				_$.sleep(function () {
					api.closeWin({
						name: winName
					})
				}, 500)
			}, 2000)

			api.closeWin({
				name: 'dingdan'
			})

		},
		clearOrder=function(){
			if(payIng===false){
				payIng = true;
				_$.ajax({
					interface: 'changeOrder',
					data: {
						type:'delete',
						id: api.pageParam.id
					},
					call: function (data, err) {
						if (data.status === 200) {
							api.toast({
								msg: '订单已删除'
							})
							getOverPage();
						}else if (data.status === 201) {
							api.toast({
								msg: data.msg
							})
						}
					}
				})
			}else{
				api.toast({
					msg: '您动作太快了，请等待接应用响应...'
				})
				return false;
			}
		},
		//下单结束的流程判断
		payOver = function (ret, err) {
			if (ret.status) {
				api.toast({
					msg: '支付成功'
				})
				getOverPage();
			} else {
				api.toast({
					msg: '抱歉，未能正常支付成功，code:' + err.code
				})
			}
			payIng=false;
		},
		payOrder = function () {
			if(payIng===false){
				payIng = true;
				_$.ajax({
					interface: 'changeOrder',
					data: {
						type:'pay',
						id: api.pageParam.id
					},
					call: function (data, err) {

						if (data.status === 201) {
							api.toast({
								msg: data.msg
							})
						} else if (data.status === 200) {

							var wxPay = api.require('wxPay'),
								orderIntro = data.data;

							if (__CONFIG__.runtime === 'dev') {
								wxPay.config({
									apiKey: 'wx5684e074db9f6b46',
									mchId: '1496614802',
									partnerKey: 'hkf34eyccrdzdzlqc1bwnnj33vuogd3y',
									notifyUrl: 'https://yikao.baolanbb.com/addons/yi_kao/inc/pay.php'
								}, function (ret, err) {
									if (ret.status) {
										wxPay.pay(orderIntro, payOver);
									} else {
										api.toast({
											msg: '抱歉，未能正常支付成功，code:' + err.code
										})
									}
								});
							} else {
								wxPay.payOrder(orderIntro, payOver);
							}
							payIng=setTimeout(function(){
								payIng=false;
							},10000)

						} else if (data.status === 202) {

							api.toast({
								msg: '课程报名成功'
							})
							getOverPage();

						} else {

							api.toast({
								msg: '抱歉，出现了一个未知错误，请联系管理员'
							})

						}
					}
				})
			}else{
				api.toast({
					msg: '您动作太快了，请等待接口响应...'
				})
				return false;
			}
		},
		initPage = function(){
			_$.ajax({
				interface:'getOrderIntro',
				data:{
					id:api.pageParam.id
				},
				call:function(data){
					if(data.status===200){
						_$.template('user_order_view_template',data.data,function(html){
							$('#user_order_view').html(html);
							$('#noready,#user_order_view').toggleClass('hide');
							_$.db.get({
								call:function(data){
									if(data.type===1){
										_$.sleep(function () {
											$('.order_btns').toggleClass('hide');
										}, 500);
									}
								}
							})
						})
					}else{
						$('#noready,#nocontent').toggleClass('hide');
					}
				}
			})
		},
		appready = function () {
			initPage();
		};
</script>
<script src="../../lib/js/config.js"></script>
<script src="../../lib/js/areaData.js"></script>
<script src="../../lib/js/jquery.js"></script>
<script src="../../lib/js/common.js"></script>
</body>
</html>